package com.wyl.clinic.controller;


import cn.hutool.core.util.ObjectUtil;
import com.wyl.clinic.common.Result;
import com.wyl.clinic.entity.Appointment;
import com.wyl.clinic.entity.DoctorWork;
import com.wyl.clinic.entity.Patient;
import com.wyl.clinic.service.AppointmentService;
import com.wyl.clinic.service.DoctorWorkService;
import com.wyl.clinic.service.PatientService;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 预约挂号单 前端控制器
 * </p>
 *
 * @author wangyulong
 * @since 2024-02-20
 */
@RestController
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private DoctorWorkService doctorWorkService;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private PatientService patientService;
    @Autowired
    private AppointmentService appointmentService;
    /**
     * 预约挂号
     * @return
     */
    @GetMapping("/add")
    @Transactional
    public Result add(@RequestParam Integer id){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Integer userId = (Integer) request.getAttribute("userId");
        //患者
        Patient patient = patientService.getByUserId(userId);
        if (ObjectUtil.isEmpty(patient)){
            return Result.fail("当前用户不是患者，无法预约");
        }
        DoctorWork doctorWork = doctorWorkService.getById(id);
        RAtomicLong availability = redissonClient.getAtomicLong("doctor:work:availability:"+doctorWork.getId());
        long remainingSlots = availability.getAndDecrement();

        if (remainingSlots > 0) {
            //生成挂号单信息
            Appointment appointment = new Appointment();
            appointment.setDoctorId(doctorWork.getDoctorId());
            appointment.setDoctorWorkId(id);
            appointment.setAppointmentDate(doctorWork.getWorkDate());
            appointment.setPatientId(patient.getId());
            appointment.setHistory(patient.getRemark());
            appointmentService.save(appointment);
            return Result.suc();
        } else {
            return Result.fail("已没有足够号源");
        }
    }

    /**
     * 取消预约
     * @return
     */
    @GetMapping("/cancel")
    public Result cancel(@RequestParam Integer id){
        Appointment appointment = appointmentService.getById(id);
        RAtomicLong availability = redissonClient.getAtomicLong("doctor:work:availability:"+appointment.getDoctorWorkId());
        availability.getAndIncrement();
        appointmentService.removeById(appointment);
        return Result.suc();
    }
}
